// ---------------------------------------------------------------------
//
// -- 王者历史面板
// -- author £
//
// ---------------------------------------------------------------------
const RadioGroup = require('common').RadioGroup,
    CommonUtil = require('CommonUtil'),
    superClass = require('AlertWindow');

cc.Class({
    extends: superClass,

    properties: {
        listContent: cc.Node,
        historyBestP: cc.Prefab,
        myBestP: cc.Prefab,
        tabGroup: cc.Node,
        rankPanelBg: cc.Node,
        btnPre: cc.Button,
        btnNext: cc.Button,
        btnReturn: cc.Node,
        tabArr: {
            default: [],
            type: cc.Toggle,
        }
    },

    ctor: function () {
        this._tabGroup = new RadioGroup();
        this._hPool = new cc.NodePool();
        this._mPool = new cc.NodePool();
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        CommonUtil.mysuper(superClass, this, 'onLoad');

        let i, len = require('UIManager').getRankItemCount();

        for (i = 0; i < len; ++i) {
            this._hPool.put(cc.instantiate(this.historyBestP));
            this._mPool.put(cc.instantiate(this.myBestP));
        }

        this._tabGroup.update(this.tabArr, this.tabGroup);
        this._tabGroup.selectedIndex = 0;
        this.rankPanelBg.height += require('UIManager').getRankBgDH();

        this.tabGroup.on('change', this.tabChangeHandler, this);
    },

    start() {
    },

    show: function () {
        CommonUtil.mysuper(superClass, this, 'show');

        switch (this._tabGroup.selectedIndex) {
            case 0:
                CommonUtil.resetPool(this._hPool, this.listContent);
                break;
            case 1:
                CommonUtil.resetPool(this._mPool, this.listContent);
                break;
        }

        this._tabGroup.selectedIndex = 0;
    },

    hide: function (data) {
        CommonUtil.mysuper(superClass, this, 'hide');
    },

    setBestHistory: function (data) {
        let list = data,
            len = list.length,
            i, item, itemC;

        CommonUtil.resetPool(this._hPool, this.listContent);

        for (i = 0; i < len; ++i) {
            if (!this._hPool.size()) break;

            item = this._hPool.get();
            this.listContent.addChild(item);
            itemC = item.getComponent(require('HistoryBestItem'));
            itemC.setData(list[i]);
        }
    },

    setMyHistory: function (data) {
        let list = data,
            len = list.length,
            i, item, itemC;

        CommonUtil.resetPool(this._mPool, this.listContent);

        for (i = 0; i < len; ++i) {
            if (!this._mPool.size()) break;

            item = this._mPool.get();
            this.listContent.addChild(item);
            itemC = item.getComponent(require('MyBestItem'));
            itemC.setData(list[i]);
        }
    },

    tabChangeHandler: function (evt) {
        if (!evt.detail.mannul) return;

        switch (this._tabGroup.selectedIndex) {
            case 0:
                CommonUtil.resetPool(this._mPool, this.listContent);
                require('MainMediator').historyBestHandler();
                break;
            case 1:
                CommonUtil.resetPool(this._hPool, this.listContent);
                require('MainMediator').myBestHandler();
                break;
        }
    },

    // update () {
    // }
});
